package com.financing.platform.web.api;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.ApiSettingDTO;
import com.financing.platform.service.IApiSettingService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.Dict;

@Controller("apiSettingActionApi")
@RequestMapping("/api/webApi")
@Scope("prototype")
public class ApiSettingAction extends BaseAction{
	
	@Autowired
	private IApiSettingService apiSettingDaoImpl;
	
	@RequestMapping("/queryApiSetting.shtml")
	@ResponseBody
	public ResultMap queryApiSetting(HttpServletRequest request,
			HttpServletResponse response) {
		try{
			resultMap.setAttribute("apiSettingList", apiSettingDaoImpl.queryApiSetting());
			resultMap.setStatus(STATUS.SUCCESS);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
		}catch(Exception e){
			log.error("query api setting error",e);
			resultMap.setStatus(STATUS.FAILE);
			resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		return resultMap;
	}
	
	@RequestMapping("/updateApiSetting.shtml")
	@ResponseBody
	public ResultMap updateApiSetting(HttpServletRequest request,
			HttpServletResponse response,ApiSettingDTO apiSettingDTO) {
			try {
				DTOdateSet.setUpdateInfo(apiSettingDTO, request.getSession());
				apiSettingDaoImpl.updateApiSetting(apiSettingDTO);
				resultMap.setAttribute(Dict.result, Dict.result_success);
				resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}catch(Exception e){
				log.error("update Api setting error!",e);
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			
			return resultMap;
	}
	
	@RequestMapping("/addApiSetting.shtml")
	@ResponseBody
	public ResultMap addApiSetting(HttpServletRequest request,
			HttpServletResponse response,ApiSettingDTO apiSettingDTO) {
		try {
				DTOdateSet.setUpdateInfo(apiSettingDTO, request.getSession());
				DTOdateSet.setCreateInfo(apiSettingDTO, request.getSession());
				
				apiSettingDaoImpl.addApiSetting(apiSettingDTO);
				resultMap.setStatus(STATUS.SUCCESS);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			}catch(Exception e){
				log.error("add Api setting error!",e);
				resultMap.setStatus(STATUS.FAILE);
				resultMap.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
			}
			
			return resultMap;
	}
	
}
